//
//  JYJVCOfMine.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/27.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfMine.h"
#import "JYJNetWorkParser.h"
#import "Masonry.h"
#import "JYJTAMacro.h"
#import "BaseLabel.h"
#import "BaseButton.h"
#import "JYJVCOfRegister.h"
#import "JYJVCOfSet.h"
#import "JYJMusicMiniView.h"
#import "BaseNavigationContoller.h"
@interface JYJVCOfMine ()
@property (nonatomic, strong) UITextField *textFieldOfPhone;
@property (nonatomic, strong)  UITextField *textFieldOfKey;
@end

@implementation JYJVCOfMine
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    self.view.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.11 alpha:1.00];
    [self configSubViews];
    
    // Do any additional setup after loading the view.
}
- (void)configSubViews {
    UIImageView *imageOfHead = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"default_user"]];
    [self.view addSubview:imageOfHead];
    [imageOfHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(30);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
        make.left.equalTo(self.view.mas_left).with.offset(SCREEN_WIDTH / 2 - 40);
    }];
    imageOfHead.layer.cornerRadius = 40;
    imageOfHead.clipsToBounds = YES;
    [imageOfHead.layer setBorderWidth:2];// 设置边界宽度
    imageOfHead.layer.borderColor = [UIColor colorWithRed:0.93 green:0.14 blue:0.14 alpha:1.00].CGColor;// 设置边界颜色
    //  设置背景图片
    UIImageView *backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageOfHead.mas_bottom).with.offset(50);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.mas_equalTo(150);
    }];
// 设置textField
    self.textFieldOfPhone = [[UITextField alloc]init];
    _textFieldOfPhone.placeholder = @"手机号码";
    _textFieldOfPhone.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    _textFieldOfPhone.textColor = [UIColor whiteColor];
    // 设置placeholder的字体颜色
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:_textFieldOfPhone.placeholder attributes:dic];
    [_textFieldOfPhone setAttributedPlaceholder:attribute];
    [backImage addSubview:_textFieldOfPhone];
    [_textFieldOfPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageOfHead.mas_bottom).with.offset(80);
        make.left.equalTo(self.view.mas_left).with.offset(62);
         make.right.equalTo(self.view.mas_right).with.offset(-62);
        make.height.mas_equalTo(40);
    }];
    self.textFieldOfKey = [[UITextField alloc]init];
    _textFieldOfKey.placeholder = @"密码";
    // 设置placeholder的字体颜色
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
    NSAttributedString *attributeKey = [[NSAttributedString alloc] initWithString:_textFieldOfKey.placeholder attributes:dict];
    [_textFieldOfKey setAttributedPlaceholder:attributeKey];
    
    _textFieldOfKey.textColor = [UIColor whiteColor];
    _textFieldOfKey.secureTextEntry = YES;
    _textFieldOfKey.returnKeyType = UIReturnKeyJoin;
    _textFieldOfKey.textColor = [UIColor whiteColor];
    [backImage addSubview:_textFieldOfKey];
    [_textFieldOfKey mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textFieldOfPhone.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(62);
        make.right.equalTo(self.view.mas_right).with.offset(-62);
        make.height.mas_equalTo(40);

    }];
    UILabel *labelOfRegister = [[UILabel alloc]init];
    labelOfRegister.text = @"注册";
    labelOfRegister.userInteractionEnabled = YES;
    labelOfRegister.textColor = [UIColor redColor];
    [backImage addSubview:labelOfRegister];
    [labelOfRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(10);
        make.top.equalTo(imageOfHead.mas_bottom).with.offset(105);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleRegister)];
    [labelOfRegister addGestureRecognizer:tap];
    
    UILabel *labelOfForgotpassword = [[UILabel alloc]init];
    labelOfForgotpassword.text = @"忘记密码";
    labelOfForgotpassword.numberOfLines = 2;
    labelOfForgotpassword.userInteractionEnabled = YES;
    labelOfForgotpassword.textColor = [UIColor redColor];
    [backImage addSubview:labelOfForgotpassword];
    [labelOfForgotpassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.top.equalTo(imageOfHead.mas_bottom).with.offset(105);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleForgotpassword)];
    [labelOfForgotpassword addGestureRecognizer:tapOne];
    
    BaseButton *buttonOfLogin = [BaseButton buttonWithType:UIButtonTypeSystem];
    [buttonOfLogin setTitle:@"登录" forState:UIControlStateNormal];
    buttonOfLogin.backgroundColor = [UIColor redColor];
    [buttonOfLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:buttonOfLogin];
    [buttonOfLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backImage.mas_bottom).with.offset(50);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
    buttonOfLogin.layer.cornerRadius = 20;
    buttonOfLogin.clipsToBounds = YES;
    [buttonOfLogin addTarget:self action:@selector(handleLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    
    

}
- (void)handleRegister {
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    JYJVCOfRegister *vcOfRegister = [[JYJVCOfRegister alloc]init];
    [self.navigationController pushViewController:vcOfRegister animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (void)handleForgotpassword {
    NSLog(@"忘记密码");
}
- (void)handleLogin:(BaseButton *)button {
    NSString *str1 = @"json=%7B%22username%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%@",self.textFieldOfPhone.text];
    NSString *str3 = @"%22%2C%22password%22%3A%22";
    NSString *str4 = [NSString stringWithFormat:@"%@",self.textFieldOfKey.text];
    NSString *str5 = BRACKETS_STR;
    string = [[[[str1 stringByAppendingString:string] stringByAppendingString:str3]stringByAppendingString:str4]stringByAppendingString:str5];
    BOOL isPhoneNum = [self isMobileNumber:self.textFieldOfPhone.text];
    if (isPhoneNum == 1) {
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/user/signin.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            NSDictionary *status = [result objectForKey:@"status"];
            NSNumber *succed =[status objectForKey:@"succeed"];
            if ([succed isEqualToNumber:@1]) {
                // 获取userDeafaults单例
                NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults];
                //登陆后把用户名和密码存储到userDeafault
                [userDeafaults setObject:self.textFieldOfPhone.text forKey:@"userName"];
                [userDeafaults setObject:self.textFieldOfKey.text forKey:@"passWord"];
                [userDeafaults setObject:result forKey:@"info"];
                [userDeafaults synchronize];
                JYJVCOfSet *set = [[JYJVCOfSet alloc]init];
                [self.navigationController pushViewController:set animated:YES];
                set.infoDic = result;
                self.navigationController.viewControllers = @[set];
                
            } else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不正确" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alert animated:YES completion:nil];
                
                [alert dismissViewControllerAnimated:YES completion:nil];
                

            }
            
            
        } andFailureBlock:^(NSError *error) {
            
        }];
        
    }else if ([self.textFieldOfPhone.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入电话号码" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        
        [alert dismissViewControllerAnimated:YES completion:nil];

    }
    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该号码尚未注册" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        
        [alert dismissViewControllerAnimated:YES completion:nil];
    }

}
// 正则判断手机号码地址格式
- (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    //    电信号段:133/153/180/181/189/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [JYJMusicMiniView shareMiniView].hidden = NO;
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
